<?php
	ini_set('display_errors', true);
	require_once(dirname(__FILE__) . "/../controllers/user.php");
	require_once(dirname(__FILE__) . "/../model/db_driver.php");
	session_start();
	if(User::getCurrentUser()==null || User::getCurrentUser()->getRuolo()<User::$ADMIN){
		echo "Errore: devi essere un amministratore per visualizzare questa pagina.<br />";
		die();
	}
	if(isset($_POST['IsPostBack'])){
		//salva i dettagli modificati
		if(isset($_POST['btnSaveDetails'])){
			if($_POST['btnSaveDetails']=='Salva'){
				$u = User::getUser($_POST['hdId']);
			}elseif($_POST['btnSaveDetails']=='Nuovo'){
				$u = new User(-1);
			}
			$u->setNome($_POST['txtNome']);
			$u->setCognome($_POST['txtCognome']);
			$u->setAlias($_POST['txtAlias']);
			$u->setEmail($_POST['txtEmail']);
			$u->setRuolo($_POST['cmbRuolo']);
			$u->setAbilitato($_POST['cmbAbilitato']);
			$u->save();
		}
	}

	if(isset($_POST["user"])){
		echo tabellaDettagli($_POST["user"]);
		die();
	}

	function tabellaUtenti(){
		$users = User::getUsers();
		
		$tab= "
		<table style='width:95%' cellpadding='0' cellspacing='0' border='0'>
			<thead>
				<tr>
					<td class='headerRow'>Id</td>
					<td class='headerRow'>Nome</td>
					<td class='headerRow'>Cognome</td>
					<td class='headerRow'>Alias</td>
					<td class='headerRow'>Email</td>
					<td class='headerRow'>Abilitato</td>
					<td class='headerRow'>Ruolo</td>
					<td class='headerRow'>&nbsp;</td>
				</tr>
			</thead>
		";
		for ($i=0;$i<count($users); $i++){
			$alt = $i % 2;
			$row_style = "rigaUtente alt_" . $alt;
			//if($users[$i]->getId()==9) var_dump($users[$i]);
			$tab = $tab . "
				<tr>
					<td class='$row_style'>" . $users[$i]->getId() ."</td>
					<td class='$row_style'>" . $users[$i]->getNome() ."</td>
					<td class='$row_style'>" . $users[$i]->getCognome() ."</td>
					<td class='$row_style'>" . $users[$i]->getAlias() ."</td>
					<td class='$row_style'>" . $users[$i]->getEmail() ."</td>
					<td class='$row_style'>" . ($users[$i]->isAbilitato()==TRUE ? "abilitato" : "disabilitato") . "</td>
					<td class='$row_style'>" . $users[$i]->getNomeRuolo() ."</td>
					<td class='cc $row_style' style='text-decoration:underline;'
							onclick='showUserDetails(" . $users[$i]->getId() .", \"dvUserDetail\")'>
						Dettagli
					</td>
				</tr>
			";
		}
		
		return $tab . "</table>";
	}
	
	function tabellaDettagli($id){
		$nome = "";
		$cognome="";
		$alias="";
		$email = "";
		$ruolo = 2;
		$abilitato = true;
		$titolo = "Crea un nuovo utente";
		$evento = "saveUserDetails();";
		if($id>=0){
			$u = User::getUser($id);
			$nome = $u->getNome();
			$cognome = $u->getCognome();
			$alias = $u->getAlias();
			$email = $u->getEmail();
			$ruolo = $u->getRuolo();
			$abilitato = $u->isAbilitato();
			$titolo = "Modifica i dettagli dell'utente";
		}else{
			$id = "Nuovo";
			$evento = "newUser();";
		}
		$details = "
		<!-- <form id='frmUserDetail' action='utenti.php' method = 'POST' > -->

		<table style='width:99%;font-size:10px;' cellpadding='0' cellspacing='0'>
			<tr>
				<td colspan='2' class='details' style='width:100%;text-align:center'>
					<p class='' style='color:green;font-weight:bold;font-size:14px;text-align:center;'> $titolo </p>
				</td>
			</tr>
			<tr>
				<td class='details ldetails'>Id</td>
				<td class='details'>$id<input type='hidden'  id='hdId'  name='hdId' value='$id' /></td>
			</tr>
			<tr>
				<td class='details ldetails'>Nome</td>
				<td class='details'><input type='text' class='txt' id='txtNome' name='txtNome' value='$nome' /></td>
			</tr>
			<tr>
				<td class='details ldetails'>Cognome</td>
				<td class='details'><input type='text' class='txt' id='txtCognome' name='txtCognome' value='$cognome' /> </td>
			</tr>
			<tr>
				<td class='details ldetails'>Alias</td>
				<td class='details'><input type='text' class='txt' id='txtAlias' name='txtAlias' value='$alias' /> </td>
			</tr>
			<tr>
				<td class='details ldetails'>Email</td>
				<td class='details'><input type='text' class='txt' id='txtEmail' name='txtEmail' value='$email' /></td>
			</tr>
			<tr>
				<td class='details ldetails'>Abilitato</td>
				<td class='details'>
					" . getComboBoxAbilitato($abilitato) . "
				</td>
			</tr>
			<tr>
				<td class='details ldetails'>Ruolo</td>
				<td class='details'>
					" . getComboBoxRuoli($ruolo) . "
				</td>
			</tr>
			<tr>
				<td colspan='2'><p>&nbsp;</p></td>
			</tr>
			<tr>
				<td align='center' stsyle='text-align:center;'><input type='button' name='btnSaveDetails' value='Salva' onclick=' $evento closeDetails();' /></td>
				<td style='text-align:right;'><input type='button' name='btnCanc' value='Chiudi' onclick='closeDetails(" . "\"dvUserDetail\"" . ")' /></td>
			</tr>
		</table>
		<!-- </form> -->
		";
		return $details;
	}
	
	function getComboBoxAbilitato($is_abilitato){
		$combo = "
			<select name='cmbAbilitato' id = 'cmbAbilitato' class='combo'>
				<option value='1' " . ($is_abilitato==true ? "selected='selected'" : "") . ">Abilitato</option>
				<option value='0' " . ($is_abilitato==false ? "selected='selected'" : "") . ">Disabilitato</option>
			";
		$combo = $combo . "</select>";
		return $combo;
	}
	
	function getComboBoxRuoli($ruolo){
		$ruoli = User::getRuoli();
		$cur_ruolo = User::getCurrentUser()->getRuolo();
		
		if($cur_ruolo <= $ruolo ){ //un utente non può modificare il ruolo di un utente più alto in grado
			$combo = "<input type='hidden' name='cmbRuolo' id='cmbRuolo' value='$ruolo'>";
			for($i=0;$i<=count($ruoli);$i++){
				if($ruolo == $i) $combo= $combo . "<span>" . $ruoli[$i] . "</span>";
			}
		}else{
			$combo = "
				<select name='cmbRuolo' id = 'cmbRuolo' class='combo'>";
				for($i = 1; $i <= count($ruoli); $i++){
					if($cur_ruolo <= $i) { break;} //un utente non può modificare ruoli più alti
					$sel = $ruolo==$i ? "selected='selected'" : "";
					$combo = $combo . "<option value= '$i' $sel >" . $ruoli[$i] . "</option>";
				}
					 
			$combo = $combo . "</select>";
		}
		return $combo;
	}

?>
	<script type='text/javascript' language='javascript'>
		
	</script>

	<style>
		td.headerRow {
			background-color:rgb(187,204,255);
			color:rgb(23,128,128);
			font-size:15px;
			border-right: solid 1px rgb(23,128,128);
			border-top: solid 1px rgb(23,128,128);
		
			padding-left: 3px;
			padding-right:3px;
			font-weight:bold;
		}
		
		td.rigaUtente  {
			color:black;
			font-size:12px;
			border-right: solid 1px rgb(23,128,128);
			border-top: solid 1px rgb(23,128,128);
			padding-left: 3px;
			padding-right:3px;
			
		}

		td.alt_0{
			background-color:rgb(207,219,236);
		}
		
		td.alt_1{
			background-color:rgb(227,233,255);		
		}
		
		td.details{
			border-bottom:ridge 1px gray;
			border-right:ridge 1px gray;
			padding-bottom:3px;
			padding-left:3px;
			padding-top:3px;
		}
		
		td.ldetails{
			width:20%;
			text-align:right;
			padding-right:5px;
		}
		
		input.txt{
			width:95%;
			font-size:10px;
		}

			
		select.combo{
			font-size:10px;
			width:95%;
		}
	</style>
	<p  class='adminTitle'>
		Gestione utenti
	</p>
	<div>
		<button id='btnNewUser' onclick='showUserDetails(-1)'>Nuovo utente</button>
	</div>
	<br />
	<br />
	<!-- stampa la tabella degli utenti -->
	<?php echo tabellaUtenti(); ?>
	
	<!-- dettagli dell'utente -->
	<div id='dvUserDetail' style='background-color:#FAFAD2;display:none;width:400px;height:280px;border: solid 1px black;
		position:absolute;top:320px;left:450px'>

		
	</div>
